<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class chartAction extends BackAction {

    function actionSex() {

        $g = new graph();

        $g->pie(60, '#505050', '{font-size: 12px; color: #404040;');

        $g->pie_values($_SESSION['chart']['sex']['v'], $_SESSION['chart']['sex']['k']);

        $g->pie_slice_colours(array('#d01f3c', '#356aa0', '#C79810'));

        $g->set_tool_tip('#val#%');

        $g->title('性别分析', '{font-size:18px; color: #d01f3c}');
        echo $g->render();
    }

    function actionLocationTop() {

        $g = new graph();

        $g->pie(60, '#505050', '{font-size: 12px; color: #404040;');

        $g->pie_values($_SESSION['chart']['location_top']['v'], $_SESSION['chart']['location_top']['k']);

        $g->pie_slice_colours(array('#d01f3c', '#356aa0', '#C79810'));

        $g->set_tool_tip('#val#%');

        $g->title('地域分析', '{font-size:18px; color: #d01f3c}');
        echo $g->render();
    }

    /**
     * 最后发表微博或评论时间
     */
    function actionLastPublish() {

        $g = new graph();

        $g->pie(60, '#505050', '{font-size: 12px; color: #404040;');

        $g->pie_values($_SESSION['chart']['last_publish']['v'], $_SESSION['chart']['last_publish']['k']);

        $g->pie_slice_colours(array('#d01f3c', '#356aa0', '#C79810'));

        $g->set_tool_tip('#val#%');

        $g->title('粉丝活跃分析', '{font-size:18px; color: #d01f3c}');
        echo $g->render();
    }
    
    /**
     * 有效粉丝分析
     */
    
    function actionUser(){
        $g = new graph();

        $g->pie(60, '#505050', '{font-size: 12px; color: #404040;');

        $g->pie_values($_SESSION['chart']['5000']['v'], $_SESSION['chart']['5000']['k']);

        $g->pie_slice_colours(array('#356aa0', '#C79810'));

        $g->set_tool_tip('#val#%');

        $g->title('粉丝有效比例', '{font-size:18px; color: #d01f3c}');
        echo $g->render();        
    }
    
    /**
     * 比例分析
     */
    function actionLimit(){
        $limit    = $this->params['limit'];
        $g = new graph();

        $g->pie(60, '#505050', '{font-size: 12px; color: #404040;');

        $g->pie_values($_SESSION['chart']['limit']['v'], $_SESSION['chart']['limit']['k'],  array("javascript:alert('http://baidu.com');"));

        $g->pie_slice_colours(array('#356aa0', '#C79810'));

        $g->set_tool_tip('#val#%');

        $g->title('粉丝比例分析'.$limit, '{font-size:18px; color: #d01f3c}');
        echo $g->render();          
    }
    
    /**
     * 用户分析
     */
    function actionAvatar(){
        $g = new graph();
        $g->pie(60, '#505050', '{font-size: 12px; color: #404040;');

        $g->pie_values($_SESSION['chart']['avatar']['v'], $_SESSION['chart']['avatar']['k']);

        $g->pie_slice_colours(array('#356aa0', '#C79810'));

        $g->set_tool_tip('#val#%');

        $g->title('粉丝自传头像比例', '{font-size:18px; color: #d01f3c}');
        echo $g->render();          
    }
    
    /**
     * 区域分析
     */
    function actionLocation(){
        $bar_red = new bar_3d( 75, '#D54C78' );
        $bar_red->key( '区域', 10 );

        // add random height bars:
        for( $i=0; $i<10; $i++ ){
          $bar_red->data[] = rand(2,5);
        }

        $bar_red->data    = $_SESSION['chart']['location']['v'];

        $g = new graph();
        $g->title( '区域百分比分析', '{font-size:20px; color: #FFFFFF; margin: 5px; background-color: #505050; padding:5px; padding-left: 20px; padding-right: 20px;}' );


        $g->data_sets[] = $bar_red;

        $g->set_x_axis_3d( count($_SESSION['chart']['location']['v']));
        $g->x_axis_colour( '#909090', '#ADB5C7' );
        $g->y_axis_colour( '#909090', '#ADB5C7' );

        $g->set_x_labels( $_SESSION['chart']['location']['k'] );
        $g->set_y_max( 100 );
        $g->y_label_steps( 10 );
        $g->set_y_legend( '', count($_SESSION['chart']['location']['k']), '#736AFF' );
        echo $g->render();
    }
    
    /**
     * 地域分析
     */
    function actionFlashLocation(){
        $weibo_id   = $this->_get_param_json_weibo_id();
        $url    = 'http://'.$_SERVER['HTTP_HOST'].'/api1/location?weibo_id='.$weibo_id.'&limit='.$num;
        $fp = file_get_contents($url);
        $fp = json_decode($fp, true);
        if($fp['code']=='error'){
            echo $fp['data'];
            exit;
        }
        $total  = $fp['total'];
        if($total==0){
            echo 'no data';
            exit;
        }
        $data   = $fp['data'];
        $k  = array();
        $v  = array();
        foreach ($data as $k1 => $v1) {
            $k[]   = $v1['location'];
            //$v[]   = $v1['nums'] / $total;
            $v[]   = $v1['nums']/ $total *100;
        }
        $_SESSION['chart']['location']['k'] = $k;
        $_SESSION['chart']['location']['v'] = $v;
        include_once 'ofc-library/open_flash_chart_object.php';
        open_flash_chart_object( '100%', 300, 'http://'. $_SERVER['HTTP_HOST'] .'/chart/location', true, url_for('misc/'));     
    }

    
    /**
     * 粉丝有效比例
     */
    function actionFlashUser(){
        $weibo_id   = $this->_get_param_json_weibo_id();
        $fp = file_get_contents('http://'.$_SERVER['HTTP_HOST'].'/api1/user?weibo_id='.$weibo_id);
        $fp = json_decode($fp);
        if($fp->code=='error'){
            echo $fp->data;
            exit;
        }
        $data   =$fp->data;
        $fetch_count    = $data->fetch_count;


        $_SESSION['chart']['5000']['k'] = array('僵尸粉丝','真实粉丝');
        $_SESSION['chart']['5000']['v'] = array((5000-$fetch_count)/5000 * 100, $fetch_count/5000 * 100);
        include_once 'ofc-library/open_flash_chart_object.php';
        open_flash_chart_object( 300, 300, 'http://'. $_SERVER['HTTP_HOST'] .'/chart/user', true, url_for('misc/'));        
    }
    
    /**
     * 需先调用粉丝有效比例分析
     */
     function actionFlashLimit(){
         
         if(isset ($this->params['limit'])){
             $limit = $this->params['limit'];
         }else{
             $limit = 200;
         }
         
        $weibo_id   = $this->params['weibo_id'];
        $url    = 'http://'.$_SERVER['HTTP_HOST'].'/api1/limit?weibo_id='.$weibo_id.'&limit='.$limit;
        $fp = file_get_contents($url);
        $fp = json_decode($fp);
        if($fp->code=='error'){
            echo $fp->data;
            exit;
        }
        
        $data   = $fp->data;
        $total  = $_SESSION['weibo_nums'];
        $_SESSION['chart']['limit']['k'] = array('其他比例','所占比例');
        $_SESSION['chart']['limit']['v'] = array((5000-$data)/5000 * 100, $data/5000 * 100);
        include_once 'ofc-library/open_flash_chart_object.php';
        open_flash_chart_object( 300, 300, 'http://'. $_SERVER['HTTP_HOST'] .'/chart/limit?limit='.$limit, true, url_for('misc/'));

    }
    
     /**
      * 上传头像比例
      */
     function actionFlashAvatar(){
         
         if(isset ($this->params['limit'])){
             $limit = $this->params['limit'];
         }else{
             $limit = 200;
         }
         
        $weibo_id   = $this->params['weibo_id'];
        $url    = 'http://'.$_SERVER['HTTP_HOST'].'/api1/avatar?weibo_id='.$weibo_id;
        $fp = file_get_contents($url);
        $fp = json_decode($fp);
        if($fp->code=='error'){
            echo $fp->data;
            exit;
        }
        $data   = $fp->data;
        $_SESSION['chart']['avatar']['k'] = array('其他','有效');
        $_SESSION['chart']['avatar']['v'] = array((5000-$fp->active)/5000 * 100, $fp->active/5000 * 100);
        include_once 'ofc-library/open_flash_chart_object.php';
        open_flash_chart_object( 300, 300, 'http://'. $_SERVER['HTTP_HOST'] .'/chart/avatar', true, url_for('misc/'));

    }
    
    
    /**
     * 粉丝性别分析
     */
    function actionFlashSex(){
        
        $weibo_id   = $this->params['weibo_id'];
        //$weibo_id   = 1915118301;
        
        $fp = file_get_contents('http://'.$_SERVER['HTTP_HOST'].'/api1/sex?weibo_id='.$weibo_id);
        $fp = json_decode($fp);
        if($fp->code=='error'){
            echo $fp->data;
            exit;
        }
        
        $data   = (array)$fp->data;

        $m  = array_values($data);
        $all    = 0;

        $new_data   = array();

        /**
         * 计算总的人数
         */
        foreach ($m as $k1 => $v1) {
            $all    = $v1 + $all;
        }
        
        foreach ($data as $k1 => $v1) {
            $new_data[]    = round($v1/$all, 4) * 100;
        }
        
        //替换性别
        $ky = array_keys($data);
        $ky_new = array();
        foreach ($ky as $k2 => $v2){
            if($v2=='f'){
                $ky_new[]   = '女';
            }else{
                $ky_new[]   = '男';
            }
        }
        
        $_SESSION['chart']['sex']['k']     = $ky_new;
        $_SESSION['chart']['sex']['v']     = $new_data;
        
        include_once 'ofc-library/open_flash_chart_object.php';
        open_flash_chart_object( 300, 300, 'http://'. $_SERVER['HTTP_HOST'] .'/chart/sex?weibo_id='.$weibo_id,true,url_for('misc/'));
    }
    
    function actionFlashLocationTop(){
        
        $weibo_id   = $this->params['weibo_id'];
        //$weibo_id   = 1915118301;
        
        $fp = file_get_contents('http://'.$_SERVER['HTTP_HOST'].'/api1/location_top?weibo_id='.$weibo_id);
        $fp = json_decode($fp);
        if($fp->code=='error'){
            echo $fp->data;
            exit;
        }
        
        $data   = $fp->data;
        //$data   = json_decode($data, true);
        $keys   = array();
        $values = array();
        foreach ($data as $k => $v) {
            $keys[]     = $v->location;
            $values[]   = $v->per;
        }
        
        $_SESSION['chart']['location_top']['k']     = $keys;
        $_SESSION['chart']['location_top']['v']     = $values;
        
        include_once 'ofc-library/open_flash_chart_object.php';
        open_flash_chart_object( 300, 300, 'http://'. $_SERVER['HTTP_HOST'] .'/chart/location_top?weibo_id='.$weibo_id,true,url_for('misc/'));
    }
    
    /**
     * 最后发表时间
     */
    function actionFlashlastPublish(){
        
        $weibo_id   = $this->params['weibo_id'];
        //$weibo_id   = 1915118301;
        
        $fp = file_get_contents('http://'.$_SERVER['HTTP_HOST'].'/api1/last_publish?weibo_id='.$weibo_id);
        $fp = json_decode($fp);
        if($fp->code=='error'){
            echo $fp->data;
            exit;
        }
        
        $data   = (array)$fp->data;

        //$data   = json_decode($data, true);
        $keys   = array();
        $values = array();
        for($i=1;$i<=7;$i++){
            $keys[]     = $i.'天';
            $values[]  = round($data['d'.$i]/ $data['fetch_count'] * 100, 2);
        }
        $keys[]     = '其他';
        $values[]  = round($data['other']/ $data['fetch_count'] * 100, 2);
        
        $_SESSION['chart']['last_publish']['v'] = $values;
        $_SESSION['chart']['last_publish']['k'] = $keys;
        
        include_once 'ofc-library/open_flash_chart_object.php';
        $fix    = implode(',', $keys) . '&value='.  implode(',', $values);
        open_flash_chart_object( 300, 300, 'http://'. $_SERVER['HTTP_HOST'] .'/chart/last_publish', true, url_for('misc/'));
    }

}

?>
